﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Quail.Helpers
{
    /// <summary>
    /// various validation cases for phone numbers
    /// </summary>
    public static class PhoneNumberValidation
    {
        public static bool IsExchangeValid(int f, int s, int t)
        {
            return ConcatDigits(s, t) != 11;
        }

        public static bool IsAreaCodeValid(int f, int s, int t)
        {
            return (!IsSameDigits(s, t)
                    && ConcatDigits(s, t) != 11
                    && ConcatDigits(f, s) != 37
                    && ConcatDigits(f, s) != 96
                    && ConcatDigits(f, s, t) != 710
                    && ConcatDigits(f, s, t) != 456);
        }

        public static bool IsSameDigits(params int[] values)
        {
            foreach (var val in values)
            {
                var checkval = val;
                if (values.Count(v => v == checkval) != 1)
                    return true;
            }

            return false;
        }

        public static int ConcatDigits(params int[] values)
        {
            var result = 0;
            var tens = 1;
            for (var idx = values.Length - 1; idx >= 0; idx--)
            {
                var v = values[idx];

                if (v < 0 || v > 9)
                    throw new Exception("Invalid parameter: " + v);

                result += (v * tens);
                tens *= 10;
            }

            return result;
        }
    }
}
